package com.hdax.dm.rest.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hdax.dm.dao.item.DmItemMapper;
import com.hdax.dm.entity.item.DmItem;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping(path = "/item")
@Api(value = "商品测试接口",tags = "商品测试接口")
public class DmItemRestService {

    @Resource
    private DmItemMapper dmItemMapper;

    @ApiOperation(value = "根据typeid查询商品信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "itemType1Id",value = "",type = "Long")
    })
    @PostMapping(path = "/hotItems")
    public List<DmItem> hotItems(@RequestParam("itemType1Id")Long itemType1Id){
        QueryWrapper<DmItem> queryWrapper = new QueryWrapper<>();
        if (itemType1Id<=8){
            queryWrapper.eq("itemType1Id",itemType1Id);
        }else {
            queryWrapper.eq("itemType2Id",itemType1Id);
        }
        queryWrapper.eq("isBanner",1L);
        queryWrapper.orderByDesc("createdTime");
        IPage<DmItem> iPage = new Page<>(0,5);
        return dmItemMapper.selectPage(iPage,queryWrapper).getRecords();
    }

    @PostMapping(path = "/getFloorOrder")
    public List<DmItem> getFloorOrder(@RequestParam("itemType1Id")Long itemType1Id,
                                      @RequestParam("limit")Long limit){
        QueryWrapper<DmItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("itemType1Id",itemType1Id);
        queryWrapper.eq("isBanner",1L);
        IPage<DmItem> iPage = new Page<>(0,limit);
        return dmItemMapper.selectPage(iPage,queryWrapper).getRecords();
    }

    @RequestMapping(path = "/findItemByType")
    public List<DmItem> findItemByType(@RequestParam("itemType1Id")Long itemType1Id){
        QueryWrapper<DmItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("itemType1Id",itemType1Id);
        return dmItemMapper.selectList(queryWrapper);
    }

    @ApiOperation(value = "根据itemid查询商品信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "itemId",value = "",type = "Long")
    })
    @PostMapping(path = "/getItems")
    public DmItem getItems(@RequestParam("itemId")Long itemId){
        return dmItemMapper.selectById(itemId);
    }

    @ApiOperation(value = "查询今日推荐")
    @ApiImplicitParams({})
    @PostMapping(path = "/recommend")
    public List<DmItem> recommend(){
        QueryWrapper<DmItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("isRecommend",1L);
        IPage<DmItem> ipage = new Page<DmItem>(0,5);
        return dmItemMapper.selectPage(ipage,queryWrapper).getRecords();
    }

    @ApiOperation(value = "查询即将开售")
    @ApiImplicitParams({})
    @PostMapping(path = "/sell")
    public List<DmItem> sell(){
        QueryWrapper<DmItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("state",2L);
        IPage<DmItem> ipage = new Page<DmItem>(0,5);
        return dmItemMapper.selectPage(ipage,queryWrapper).getRecords();
    }

    //根据年龄段查询商品信息
    @RequestMapping(path = "/byAgeGroup")
    public List<DmItem> byAgeGroup(@RequestParam("ageGroup")Long ageGroup){
        QueryWrapper<DmItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ageGroup",ageGroup);
        IPage<DmItem> ipage = new Page<DmItem>(0,6);
        return dmItemMapper.selectPage(ipage,queryWrapper).getRecords();
    }

}
